package com.zhangj.service.impl;


import com.zhangj.mapper.UserDAO;
import com.zhangj.pojo.User;
import com.zhangj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Description
 * @Author zhangjie
 * @Date 2022/1/11 18:22
 * @Version 1.0
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    UserDAO userDAO;

    @Resource
    RedisTemplate<String,List<User>> redisTemplate;

    @Override
    public List<User> findUsers() {
        List<User> users = redisTemplate.boundValueOps("allUser").get();

        if (users == null) {
           users = userDAO.selectAll();
           redisTemplate.boundValueOps("allUser").set(users);
        }else{
            System.out.println("redis=======");
        }
        return users;
    }

    @Override
    public User findUserById(Long id) {
        return userDAO.selectByPrimaryKey(id);
    }

    @Override
    public void saveUser(User user) {
        userDAO.insertSelective(user);
    }

    @Override
    public void updateUser(User user) {
        userDAO.updateByPrimaryKeySelective(user);
    }

    @Override
    public void deleteUserById(Long id) {
        userDAO.deleteByPrimaryKey(id);
      //  throw new RuntimeException();
    }
}
